<?php
/**
 * Created by PhpStorm.
 * User: huangpeng3
 * Date: 2018/1/28
 * Time: 下午3:30
 */
namespace Home\Controller;

use Think\Controller;

Class ExchangeController extends Controller
{

    public function add()
    {
        $rst = array();
        if (!empty($_SESSION['user_info']['id'])) {
            $uid = intval($_SESSION['user_info']['id']);
            if (isset($_POST['eid'], $_POST['akey'], $_POST['skey'], $_POST['label'])) {
                $eid = intval($_POST['eid']);
                $akey = $_POST['akey'];
                $skey = $_POST['skey'];
                $label = $_POST['label'];
                $ex = D('Exchange');
                $flag = $ex->addExchange($uid, $eid, $akey, $skey, $label);
                if ($flag == 200) {
                    $rst['status'] = true;
                    $rst['info'] = '添加成功';
                } elseif ($flag == -1) {
                    $rst['status'] = false;
                    $rst['info'] = '添加失败';
                    $rst['flag'] = C('COMMON_ERROR');
                } elseif ($flag == -2) {
                    $rst['status'] = false;
                    $rst['info'] = '重复添加';
                    $rst['flag'] = C('COMMON_DUPLICATE_ERROR');
                }
            } else {
                $rst['status'] = false;
                $rst['info'] = '参数不全';
                $rst['flag'] = C('PARAM_LESS_ERROR');
            }
        } else {
            $rst['status'] = false;
            $rst['info'] = '未登录';
            $rst['flag'] = C('UNLOGIN_ERROR');
        }
        echo json_encode($rst);
    }

    public function del()
    {
        $rst = array();
        if (!empty($_SESSION['user_info']['id'])) {
            if (isset($_POST['id']) && $_POST['eid']) {
                $pid = intval($_POST['id']);
                $uid = intval($_SESSION['user_info']['id']);
                $eid = intval($_POST['eid']);
                $ex = D('Exchange');
                $flag = $ex->delExchange($pid, $uid, $eid);
                if ($flag == 200) {
                    $rst['status'] = true;
                    $rst['info'] = '删除成功';
                } elseif ($flag == -1) {
                    $rst['status'] = false;
                    $rst['info'] = '删除失败';
                    $rst['flag'] = C('COMMON_ERROR');
                }
            } else {
                $rst['status'] = false;
                $rst['info'] = '参数不全';
                $rst['flag'] = C('PARAM_LESS_ERROR');
            }
        } else {
            $rst['status'] = false;
            $rst['info'] = '未登录';
            $rst['flag'] = C('UNLOGIN_ERROR');
        }
        echo json_encode($rst);
    }

    public function update()
    {
        $rst = array();
        if (!empty($_SESSION['user_info']['id'])) {
            $uid = intval($_SESSION['user_info']['id']);
            if (isset($_POST['id']) && isset($_POST['eid'], $_POST['akey'], $_POST['skey'], $_POST['label'])) {
                $pid = intval($_POST['id']);
                $eid = intval($_POST['eid']);
                $akey = $_POST['akey'];
                $skey = $_POST['skey'];
                $label = $_POST['label'];

                $ex = D('Exchange');
                $flag = $ex->updateExchange($pid, $uid, $eid, $akey, $skey, $label);
                if ($flag == 200) {
                    $rst['status'] = true;
                    $rst['info'] = '更新成功';

                } elseif ($flag == -1) {
                    $rst['status'] = false;
                    $rst['info'] = '更新失败';
                    $rst['flag'] = C('COMMON_ERROR');
                }
            } else {
                $rst['status'] = false;
                $rst['info'] = '参数不全';
                $rst['flag'] = C('PARAM_LESS_ERROR');
            }
        } else {
            $rst['status'] = false;
            $rst['info'] = '未登录';
            $rst['flag'] =  C('UNLOGIN_ERROR');
        }
        echo json_encode($rst);
    }

    public function listAll()
    {
        $rst = array();
        if (!empty($_SESSION['user_info']['id'])) {
            $uid = intval($_SESSION['user_info']['id']);
            $ex = D('Exchange');
            $flag = $ex->getAll($uid);
            if ($flag != -1) {
                $rst['status'] = true;
                $rst['info'] = '查询成功';
                $rst['data'] = $flag;

            } else {
                $rst['status'] = false;
                $rst['info'] = '查询失败';
                $rst['flag'] =C('COMMON_ERROR');
            }

        } else {
            $rst['status'] = false;
            $rst['info'] = '未登录';
            $rst['flag'] = C('UNLOGIN_ERROR');
        }
        echo json_encode($rst);
    }

}